float4x4 StateWorldView;
float4x4 StateComplete;

float4 color = { 1.0, 0.5, 0.0, 1.0 };
const float ambient = 0.3;
const float diffuse = 0.7;
const float specular = 1;
const float glossiness = 20.0;
const float3 lightPosition = { 100, 100, 100 };



struct VSInput
{
	float4 Position: POSITION;
	float2 Texcoord: TEXCOORD0;
	float3 Normal: NORMAL;
	float3 Binormal: TEXCOORD2;
	float3 Tangent: TEXCOORD3;
};

struct VSOutput
{
	float4 Position: POSITION;
	float3 Normal: TEXCOORD2;
	float3 ViewVector: TEXCOORD1;
	float3 LightVector: TEXCOORD0;
};

VSOutput vsMain(VSInput Input)
{
	VSOutput Output;
	
	Output.Position = mul(StateComplete, Input.Position);
	Output.Normal = mul(float3x3(StateWorldView), Input.Normal);
	Output.ViewVector = mul(StateWorldView, Input.Position).xyz;
	Output.LightVector = lightPosition - Output.ViewVector;
	
	return Output;
}

float4 psMain(VSOutput Input): COLOR
{
	Input.LightVector = normalize(Input.LightVector);
	float3 reflectedViewVector = normalize(reflect(Input.ViewVector, Input.Normal));
	
	float diffuseLight = max(0,dot(Input.Normal, Input.LightVector));
	float specularLight = pow(max(0,dot(reflectedViewVector, Input.LightVector)), glossiness);
	
	float fresnel = pow(1-max(0, dot(normalize(Input.Normal), normalize(Input.ViewVector))),3);
	return float4((ambient + diffuseLight*diffuse) * (color.xyz + specularLight*specular), color.w) + fresnel;
}